import 'package:flutter/material.dart';

class GridViewPage2 extends StatefulWidget {
  @override
  _GridViewPage2State createState() => _GridViewPage2State();
}

class _GridViewPage2State extends State <GridViewPage2> {

  List<IconData> _icons = [];

  @override
  void initState() {
    super.initState();
    _retrieveIcons();
  }

  void _retrieveIcons() {
    Future.delayed(
      Duration(
        milliseconds: 200
      )
    ).then((e) {
      setState(() {
        _icons.addAll([
          Icons.ac_unit,
          Icons.airport_shuttle,
          Icons.all_inclusive,
          Icons.beach_access,
          Icons.cake,
          Icons.free_breakfast
        ]);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Infinite Grid View"),
      ),
      body: Container(
        color: Colors.red,
        child: Scrollbar(
          child: GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              childAspectRatio: 1.0
            ),
            itemCount: _icons.length,
            itemBuilder: (context, index) {
              if (index == _icons.length - 1 && _icons.length < 100) {
                _retrieveIcons();
              }

              return Icon(_icons[index]);
            },
          ),
        ),
      )
    );
  }
}